//界面初始化值
var reloadFlag = true;
var fileName = "";
var isDebug = false;
var turnOnTime = 24;//单位为小时
var showModul = "PCT_TV_SHOW";
var un = localStorage.getItem('un');
var pw = localStorage.getItem('pw');
var serverUrl = localStorage.getItem('serverUrl');
var serverIp = localStorage.getItem('serverIp');
var serverPort = localStorage.getItem('serverPort');
var serverProject = localStorage.getItem('serverProject');

var type = "workCenter";
var workCenterGid = localStorage.getItem('workcenterGid');
var workCenterCode = localStorage.getItem('workcenterCode');
var workCenterName = localStorage.getItem('workcenterName');
var userName = localStorage.getItem('un');
var nowDate;
var nowDateStart;
var siteGid = '2c942a6a50c620400150c62b5814000a';
var siteCode = '01';
var siteName = '金属事业部';
var typeSite = "factory";
var containerWith;
//消息重连定时器对象
var webSocketTimer = null;
//消息url前半段
var msgUrl = "ws://" + serverIp + ":" + serverPort + "/" + serverProject + "/webmsg/";
//消息socket
var socket = atmosphere;
var equipLineChart;
var qualityPerChart;
var bgImag;
apiready = function() {
	//横竖屏自动适配 （auto）  横屏（landscape_left） auto_landscape
	api.setScreenOrientation({
		orientation : 'landscape_left'
	});
	
	api.addEventListener({
	    name:'keyback'
    },function(ret,err){
    	if( ret ){			   
	        api.closeToWin({
			    name: 'main_win'
			});
			api.setScreenOrientation({
	            orientation: 'auto'
	        });
	    }else{
	        alert( JSON.stringify( err ) );
	    }
    });
	//工作中心赋值
	$("#workCenterName").html("线体："+workCenterName);
	// 获取屏幕宽度
	containerWith =	$(".container").width();
	//初始化数据
	getRemoteData();
	qualityPerChart = echarts.init(document.getElementById('qualityPer'));
	equipLineChart = echarts.init(document.getElementById('equipLine'));
	var version = api.appVersion;
	$("#version").html("version：" + version);
	nowDateStart = localStorage.getItem('nowDate');
	bgImag = localStorage.getItem('bgImag');
	console.log(bgImag);
	if(bgImag!='' || bgImag!=undefined){
		$("body").css({
		  "background" :"url("+bgImag+")"
	   });
	}

}


$(function(){
	$("#js-logo").click(function(){
	    $(".scrollcon").slideToggle("slow");
	});
	
	$(".scroll ul li").click(function(){
	  var simg= $(this).find("img").attr("src");
	  localStorage.setItem('bgImag', simg);
	  //var bimg=simg.replace(/-\d*/,"");
	  $("body").css({
		  "background" :"url("+simg+")"
	  });
	});
	
});


//获取副屏数据
function getRemoteData() {
	var url = "http://" + serverIp + ":" + serverPort + "/" + serverProject
			+ "/autoLoginController!login.m?_u=" + un + "&_p=" + pw
			+ "&_to=pctLayoutController6!getShowData.m&type=" + type
			+ "&typeKey=" + workCenterGid + "&typeValue=" + workCenterCode
			+ "&userName=" + userName + "&layoutShow=" + showModul;
			api.ajax({
					url : url,
					method : 'post',
					dataType : 'json',
					data : {
						values : {
							nowTime : new Date()
						}
					}
			}, function(ret, err) {
			var data = eval(ret);
			
			nowDate = data.nowDate;	

			turnOnTime = data.turnOnTime;
			var equipStatusKanbanDataMap = data.equipStatusKanbanDataMap;
			var equipOEEDataMap = data.equipOEEDataMap;
			var equipFinishDataMap = data.equipFinishDataMap;			
			var equipOp = data.equipOp;
			
			// 异常信息
			var unusualAlarmMap = data.unusualAlarmMap;
			if (null != unusualAlarmMap){
				setLiValue(unusualAlarmMap.wrongs,unusualAlarmMap.wrongQty,unusualAlarmMap.dealingQty);
			}

			//机型
			var mrl = data.mrl;
			if(checkNull(mrl)){
				setEquipType(mrl);
			}
			//通知模板
			var tvState = data.tvState;
			setTvStata(tvState);
			var tvContent = data.tvContent;
			if(data.syncTvContent)
				setTvContent(tvContent);

			//设备状态
			var stateMap = data.stateMap;
			if(checkNull(stateMap))
				setEquipmentState(stateMap);
			
			// 质量合格不合格数据	
			var uqcDataMap = data.uqcDataMap;
			if(checkNull(uqcDataMap))
				uqcChartInit(uqcDataMap);	
			
			//责任人
			var dutyManDataMap = data.dutyManDataMap;
			if(checkNull(dutyManDataMap))
				setDutyMan(dutyManDataMap);

			//加工信息
			var processMsgDataList = data.processMsgDataList;
			if(checkNull(processMsgDataList))
				setProcessMsg(processMsgDataList);

			//设备工艺信息
			if(checkNull(equipOp)){
				$.each(equipOp, function(i) {
					var item = equipOp[i];
					$.each(svgDataArray, function(j) {
						if (item.equipName.indexOf(svgDataArray[j]) != -1) {
							var index = j < 9 ? "0" + (j + 1) : (j + 1);
							var op = document.getElementById("op" + index);
							op.textContent = item.opName;
						}
					});
				});
			}

			//物料管理			
			var mrlStateList = data.mrlStateList;
			var planCompMap = data.planCompMap;
			if(checkNull(mrlStateList) && checkNull(planCompMap))
				getMrlTableInfo(mrlStateList,planCompMap);

			dateShow();
			if(checkNull(equipStatusKanbanDataMap))
				changeEquipSvgData(equipStatusKanbanDataMap.dataList);
			RBtest(data);
			if(checkNull(equipOEEDataMap) && checkNull(equipFinishDataMap))
				equipLine(equipOEEDataMap,equipFinishDataMap);
		});
		RotateSVG();	
}

//获取固定格式的日期
function getFormatDate(date) {
	var year = date.getFullYear();
	var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1);
	var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
	var hour = date.getHours() > 9 ? date.getHours() : "0" + date.getHours();
	var minute = date.getMinutes() > 9 ? date.getMinutes() : "0" + date.getMinutes();
	var second = date.getSeconds() > 9 ? date.getSeconds() : "0" + date.getSeconds();
	var formatStr = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
	return formatStr;
}
//时间显示赋值
function dateShow() {
	var timeInterval = setInterval(function() {
		//按秒相加
		nowDate = new Date(Date.parse(nowDate) + (1000));
		var formatStr = getFormatDate(nowDate);
		var nowTime = document.getElementById('time');

		var date3 = Date.parse(nowDate)-Date.parse(nowDateStart);
		
		var days=Math.floor(date3/(24*3600*1000));
		var leave1=date3%(24*3600*1000);    //取余数
		var hours=Math.floor(leave1/(3600*1000));
		var leave2=leave1%(3600*1000);       //取余数
		var minutes=Math.floor(leave2/(60*1000));
		//计算相差秒数
		var leave3=leave2%(60*1000);     //取余数
		var seconds=Math.round(leave3/1000);
		
		if(seconds>=60){
				seconds = 0;
				minutes = minutes +1;
				if(minutes>=60){
					minutes = 0;
					hours = hours + 1;
					if(hours>=24){
						hours = 0;
						days = days + 1;
					}
				}
		}
		
		if(turnOnTime<=days*24+hours)
		{
			var formatStr2 = "开机超过: " + days + " 天 " + hours + " 小时,请注意电视保养 ";
		}else
		{
			if(days>=0 && hours>=0 && minutes>=0)
			{
				var formatStr2 = "开机时长: " + days + " 天 " + hours + " 小时 " + minutes + " 分钟 ";				
			}else
			{
				var formatStr2 = "开机时长: " + 0 + " 天 " + 0 + " 小时 " + 0 + " 分钟 ";
			}
		}
		var nowTime2 = document.getElementById('startTime');
		nowTime2.innerHTML = formatStr2;	
	
		nowTime.innerHTML = formatStr;
	}, 1000);
}

var svgDataArray;
function changeColor(id, state, text) {
	// -1 离线 灰色;0 空闲黄色;1 运行 绿色;3 报警 红色;
	var png = 'picCNCnull.png';
	var im = document.getElementById("p" + id);
	var it = document.getElementById("t" + id);

	if (state == '1') {
		png = 'CNC2-green.png';
	} else if (state == '3') {
		png = 'CNC2-red.png';
	} else if (state == '-1') {
		png = 'CNC2-gray.png';
	} else if (state == '0') {
		png = 'CNC2-yellow.png';
	} else {
		png = 'picCNCnull.png';
	}
	$("#p"+id).attr('src','../image/'+png+'');
	it.textContent = text;
	
};

//获取工作中心
function getWorkCenterCode() {
	return workCenterCode;
}

//改变RGV颜色
function changeColorRobotB(id, state) 
{
	// -1 离线 灰色;0 空闲 黄色;1 运行 蓝色;3 报警 红色;
	var png2 = 'jxs-white';
	if (state == '1') 
	{
		png2 = 'jxs-green.png';
	} else if (state == '3') 
	{
		png2 = 'jxs-red.png';
	} else if (state == '-1') 
	{
		png2 = 'jxs-gray.png';
	} else if (state == '0') 
	{
		png2 = 'jxs-yellow.png';
	}else
	{
		png2 = 'NULL.png';
	}

	$("#rb"+id).attr('src','../image/'+png2+'');
	
};
//改变机器手臂颜色
function changeColorRobotA(id, state) 
{
	// -1 离线 灰色;0 空闲黄色;1 运行 蓝色;3 报警 红色;
	var png3 = 'jxs-white';
	if (state == '1') 
	{
		png3 = 'jxs-green.png';
	} else if (state == '3') 
	{
		png3 = 'jxs-red.png';
	} else if (state == '-1') 
	{
		png3 = 'jxs-gray.png';
	} else if (state == '0') 
	{
		png3 = 'jxs-yellow.png';
	}else
	{
		png2 = 'NULL.png';
	}
	$("#rb"+id).attr('src','../image/'+png3+'');
};

function RBtest(data)
{
  var thisLine= getLineName();
  //A
  var data1=[{"ROBOTNAME":"A01","STATUSCODE":"1"},{"ROBOTNAME":"A02","STATUSCODE":"1"},{"ROBOTNAME":"A03","STATUSCODE":"1"},{"ROBOTNAME":"A04","STATUSCODE":"1"},{"ROBOTNAME":"A05","STATUSCODE":"1"},{"ROBOTNAME":"A06","STATUSCODE":"1"},{"ROBOTNAME":"A07","STATUSCODE":"1"},{"ROBOTNAME":"A08","STATUSCODE":"1"}];
  //B
  var data2=[{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"B02","STATUSCODE":"1"},{"ROBOTNAME":"B03","STATUSCODE":"1"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"E06","STATUSCODE":"1"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"B10","STATUSCODE":"1"},{"ROBOTNAME":"B11","STATUSCODE":"1"}];
  //C
  var data3=[{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"B02","STATUSCODE":"1"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"B04","STATUSCODE":"1"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"E06","STATUSCODE":"1"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"B10","STATUSCODE":"1"},{"ROBOTNAME":"B11","STATUSCODE":"1"}];
  //D
  var data4=[{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"B02","STATUSCODE":"1"},{"ROBOTNAME":"B03","STATUSCODE":"1"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"E06","STATUSCODE":"1"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"B08","STATUSCODE":"1"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"B10","STATUSCODE":"1"},{"ROBOTNAME":"-","STATUSCODE":"-"}];
  //E
  var data5=[{"ROBOTNAME":"B01","STATUSCODE":"1"},{"ROBOTNAME":"B02","STATUSCODE":"1"},{"ROBOTNAME":"B03","STATUSCODE":"-1"},{"ROBOTNAME":"B04","STATUSCODE":"-1"},{"ROBOTNAME":"B05","STATUSCODE":"1"},{"ROBOTNAME":"E06","STATUSCODE":"1"},{"ROBOTNAME":"B07","STATUSCODE":"1"},{"ROBOTNAME":"B08","STATUSCODE":"1"},{"ROBOTNAME":"B09","STATUSCODE":"1"},{"ROBOTNAME":"B10","STATUSCODE":"1"},{"ROBOTNAME":"B11","STATUSCODE":"-1"}];
  //F
  var data6=[{"ROBOTNAME":"B01","STATUSCODE":"1"},{"ROBOTNAME":"B02","STATUSCODE":"1"},{"ROBOTNAME":"B03","STATUSCODE":"1"},{"ROBOTNAME":"B04","STATUSCODE":"1"},{"ROBOTNAME":"B05","STATUSCODE":"1"},{"ROBOTNAME":"E06","STATUSCODE":"1"},{"ROBOTNAME":"B07","STATUSCODE":"1"},{"ROBOTNAME":"B08","STATUSCODE":"1"},{"ROBOTNAME":"B09","STATUSCODE":"1"}];
  //G
  var data7=[{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"B02","STATUSCODE":"1"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"B04","STATUSCODE":"1"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"E06","STATUSCODE":"1"},{"ROBOTNAME":"B07","STATUSCODE":"1"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"B09","STATUSCODE":"1"}];
  //H
  var data8=[{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"B02","STATUSCODE":"1"},{"ROBOTNAME":"B03","STATUSCODE":"1"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"B05","STATUSCODE":"1"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"B08","STATUSCODE":"1"},{"ROBOTNAME":"B09","STATUSCODE":"1"}];
  //I
  var data9=[{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"B02","STATUSCODE":"1"},{"ROBOTNAME":"B03","STATUSCODE":"1"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"B05","STATUSCODE":"1"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"B08","STATUSCODE":"1"},{"ROBOTNAME":"B09","STATUSCODE":"1"},{"ROBOTNAME":"-","STATUSCODE":"-"},{"ROBOTNAME":"-","STATUSCODE":"-"}];
  //J
  var data10=[{"ROBOTNAME":"B01","STATUSCODE":"1"},{"ROBOTNAME":"B02","STATUSCODE":"1"},{"ROBOTNAME":"B03","STATUSCODE":"1"},{"ROBOTNAME":"B04","STATUSCODE":"1"},{"ROBOTNAME":"B05","STATUSCODE":"1"},{"ROBOTNAME":"E06","STATUSCODE":"1"},{"ROBOTNAME":"B07","STATUSCODE":"1"},{"ROBOTNAME":"B08","STATUSCODE":"1"},{"ROBOTNAME":"B09","STATUSCODE":"-1"}];
  if("A"==thisLine)
  {
		for (var i = 0; i < data1.length; i++) 
		{
			if (i < 9) {
				changeColorRobotA("0" + (i + 1), data1[i].STATUSCODE);
			} else {
				changeColorRobotA((i + 1), data1[i].STATUSCODE);
			}
		}
	} else if ("B" == thisLine) {
		for (var i = 0; i < data2.length; i++) 
		{
			if (i < 9) 
			{
				changeColorRobotB("0" + (i + 1), data2[i].STATUSCODE);
			} else {
				changeColorRobotB((i + 1), data2[i].STATUSCODE);
			}
		}
	} else if ("C" == thisLine) {
		for (var i = 0; i < data3.length; i++) 
		{
			if (i < 9) 
			{
				changeColorRobotB("0" + (i + 1), data3[i].STATUSCODE);
			} else {
				changeColorRobotB((i + 1), data3[i].STATUSCODE);
			}
		}
	} else if ("D" == thisLine) {
		for (var i = 0; i < data4.length; i++) 
		{
			if (i < 9) 
			{
				changeColorRobotB("0" + (i + 1), data4[i].STATUSCODE);
			} else {
				changeColorRobotB((i + 1), data4[i].STATUSCODE);
			}
		}
	} else if ("E" == thisLine) {
		for (var i = 0; i < data5.length; i++) 
		{
			if (i < 9) 
			{
				changeColorRobotA("0" + (i + 1), data5[i].STATUSCODE);
			} else {
				changeColorRobotA((i + 1), data5[i].STATUSCODE);
			}
		}
	} else if ("F" == thisLine) {
		for (var i = 0; i < data6.length; i++) 
		{
			if (i < 9) 
			{
				changeColorRobotA("0" + (i + 1), data6[i].STATUSCODE);
			} else {
				changeColorRobotA((i + 1), data6[i].STATUSCODE);
			}
		}
	} else if ("G" == thisLine) {
		for (var i = 0; i < data7.length; i++) 
		{
			if (i < 9) 
			{
				changeColorRobotB("0" + (i + 1), data7[i].STATUSCODE);
			} else {
				changeColorRobotB((i + 1), data7[i].STATUSCODE);
			}
		}
	} else if ("H" == thisLine) {
		for (var i = 0; i < data8.length; i++) 
		{
			if (i < 9) 
			{
				changeColorRobotB("0" + (i + 1), data8[i].STATUSCODE);
			} else {
				changeColorRobotB((i + 1), data8[i].STATUSCODE);
			}
		}
	} else if ("I" == thisLine) {
		for (var i = 0; i < data9.length; i++) 
		{
			if (i < 9) 
			{
				changeColorRobotB("0" + (i + 1), data9[i].STATUSCODE);
			} else {
				changeColorRobotB((i + 1), data9[i].STATUSCODE);
			}
		}
	} else if ("J" == thisLine) {
		for (var i = 0; i < data10.length; i++) 
		{
			if (i < 9) 
			{
				changeColorRobotA("0" + (i + 1), data10[i].STATUSCODE);
			} else {
				changeColorRobotA((i + 1), data10[i].STATUSCODE);
			}
		}
	}
}

//获取线名
function getLineName() {
	return workCenterName.substring(0, 1);
}

//机台状态  -1 离线 灰色;0 空闲蓝色;1 运行 绿色;2 暂停 黄色;3 报警 红色;
function changeEquipSvgData(data) {

	var dataArray = $.parseJSON(data);
	//遍历dataArray
	for (var i = 0; i < dataArray.length; i++) 
	{
		for (var j = 0; j < svgDataArray.length; j++) 
		{
			if (dataArray[i].EQUIPNAME.indexOf(svgDataArray[j]) > 0) {			
				if (j < 9) {
					changeColor("0" + (j + 1), dataArray[i].STATUSCODE, getLineName() + svgDataArray[j]);
				} else {
					changeColor((j + 1), dataArray[i].STATUSCODE, getLineName() + svgDataArray[j]);
				}
				break;
			}
		}

	}
}

function RotateSVG() 
{	
	var lineA=new Array("02","04","06","08","10","12","14","16","-","-","-","01","03","05","07","09","11","13","15","-","-","-");
    var lineB=new Array("02","04","06","08","10","12","14","16","18","20","22","01","03","05","07","09","11","13","15","17","19","21");
    var lineC=new Array("02","04","06","08","10","12","14","16","18","20","22","-","01","03","-","05","07","-","09","11","-","13");
    var lineD=new Array("-","02","04","-","06","08","-","10","12","-","14","01","03","05","07","09","11","13","15","17","19","21");
    var lineE=new Array("02","04","06","08","10","12","14","16","18","20","22","01","03","05","07","09","11","13","15","17","19","21");
    var lineF=new Array("01","03","05","07","09","11","13","15","17","-","-","02","04","06","08","10","12","14","16","18","-","-");
    var lineG=new Array("01","03","05","07","09","11","13","15","17","-","-","02","04","06","08","10","12","14","16","18","-","-");
    var lineH=new Array("-","01","03","-","05","07","-","09","11","-","-","02","04","06","08","10","12","14","16","18","-","-");
    var lineI=new Array("-","02","04","-","06","08","-","10","12","-","-","01","03","05","07","09","11","13","15","17","-","-");
    var lineJ=new Array("01","03","05","07","09","11","13","15","17","-","-","02","04","06","08","10","12","14","16","18","-","-");
    var lineK=new Array("01","03","05","07","09","11","13","15","17","-","-","02","04","06","08","10","12","14","16","18","-","-");
	var line = getLineName();
	if (line == "A") {
		svgDataArray = lineA;
		if(containerWith<=912){
			$("#fgx11").width(340);
			$("#fgx22").width(340);
		}else{
			$("#fgx11").width(480);
			$("#fgx22").width(480);
		}
		
	} else if (line == "B") {
		svgDataArray = lineB;
	} else if (line == "C") {
		svgDataArray = lineC;
	} else if (line == "D") {
		svgDataArray = lineD;
	} else if (line == "E") {
		svgDataArray = lineE;
	} else if (line == "F") {
		svgDataArray = lineF;
		if(containerWith<=912){
			$("#fgx11").width(390);
			$("#fgx22").width(390);
		}else{
			$("#fgx11").width(550);
			$("#fgx22").width(550);
		}
	} else if (line == "G") {
		svgDataArray = lineG;
		if(containerWith<=912){
			$("#fgx11").width(390);
			$("#fgx22").width(390);
		}else{
			$("#fgx11").width(550);
			$("#fgx22").width(550);
		}
	} else if (line == "H") {
		svgDataArray = lineH;
		if(containerWith<=912){
			$("#fgx11").width(390);
			$("#fgx22").width(390);
		}else{
			$("#fgx11").width(550);
			$("#fgx22").width(550);
		}
	} else if (line == "I") {
		svgDataArray = lineI;
		if(containerWith<=912){
			$("#fgx11").width(390);
			$("#fgx22").width(390);
		}else{
			$("#fgx11").width(550);
			$("#fgx22").width(550);
		}
	} else if (line == "J") {
		svgDataArray = lineJ;
		if(containerWith<=912){
			$("#fgx11").width(390);
			$("#fgx22").width(390);
		}else{
			$("#fgx11").width(550);
			$("#fgx22").width(550);
		}
	} else if (line == "K") {
		svgDataArray = lineK;
		if(containerWith<=912){
			$("#fgx11").width(390);
			$("#fgx22").width(390);
		}else{
			$("#fgx11").width(550);
			$("#fgx22").width(550);
		}
	}
	//遍历dataArray
	for (var i = 0; i < svgDataArray.length; i++) 
	{
		if (svgDataArray[i] != "-") {
			if (i < 9) {
				changeColor("0" + (i + 1), "-1", line + svgDataArray[i]);
			} else {
				changeColor((i + 1), "-1", line + svgDataArray[i]);
			}
		}
	}
}

//加工信息
function setProcessMsg(data) {
	//转换jason格式
	var dataObj = eval(data);
	$("#opImag").children().remove();
	//遍历数组
	$.each(dataObj, function(j,item) {
		var opName_=$("#CNC_"+j);
		$(opName_).text(item.opName);
		var $DIV = $("<div class='col-xs-4 col-sm-4 col-md-4 col-lg-4'></div>");
		var $IMG = $("<img col-xs-3 col-sm-3 col-md-3 col-lg-3 class='gyImg' src='" + "http://" + serverIp + ":" + serverPort+ "/" + serverProject + "/" + item.opImg + "' />");
		$DIV.append($IMG);
		$("#opImag").append($DIV);
	});
}

$(function(){
	$('#opImag').zoomify();
	 /*if(bgImag!="" || bgImag!=undefined){
		  $("body").css({
		  	"background" :"url("+bgImag+")"
		  });	  
	  }*/
});

//设备稼动率 
function equipLine(data,data2) 
{	
	var xAxisData = data.xAxis;
	var finishData = data2.mapData;
	
	for(var i = 0;i<xAxisData.length;i++)
	{
		xAxisData[i] = xAxisData[i].substring(5);
	}
	option = {
		tooltip : {
	        trigger: 'axis',
	        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
	            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
	        }
	    },
	 	grid:{
			top : '13%',
			bottom : '15%',
		},
	    legend: {
	    	top : '2%',
	        data: ['稼动率', '完工数'],
	         textStyle: {
	            color:'#fff',
	        }
	    },
	    calculable : true,	  
	    xAxis: [{	    
	        type: 'category',	       
            axisTick: {
            	show: false,
            	lineStyle: {
            		color:'#fff',
            	},
            },
	        axisLine: {
	            lineStyle: {
	                color: '#fff',
	            },      
	        },
	        splitLine: {show:false},
			axisLabel : {
				'interval' : 0,
				textStyle: {
                    color: '#fff',
                }
			},       
	        data: xAxisData	        
	    }],	   
	    yAxis: [{
	        type: 'value',
	       	interval:0,
	       	min: 0,	       	
	       	axisTick: {
	       		show: false,
            	lineStyle: {
            		color:'#fff',
            	},
            },
	        axisLabel: {
	            show: true,
	            formatter : '{value} %',
                textStyle: {
                    color: '#fff'
                },
	        },
	        axisLine: {	        	
	            lineStyle: {
	                color: '#fff',
	            },	           
	        },
	        splitLine: {
                show:true,
                lineStyle: {
                    color: ['#7A7D84'],
                },
            },
	    }, {
	        type: 'value',
	        min: 0,
	        axisTick: {
	        	show:false,
            	lineStyle: {
            		color:'#fff',
            	},
            },
            axisLabel: {
	            show: true,
                textStyle: {
                    color: '#fff'
                }
	        },
	        position: 'right',
	        splitLine:{
	           show: false,
	        },
	        axisLine: {
	            lineStyle: {
	                color: '#fff',              
	            },
	             
	        },

	    }],
	    series: [{
	        name: '稼动率',
	        type: 'line',	        	              
	        itemStyle: {
	            normal: {
	                color:'#56ED1E',
	            }
	        },
	        label: {
	            normal: {
	                show: true,
	                position: 'top',
	                color:'#fff',
	            }
	        },	 	        
	        data: data.series[0].data
	        //data:[45, 94, 62, 89]
	    }, {
	        name: '完工数',
	        type: 'bar',
	        barWidth: 30,
	        color:'ED7C30',
	        yAxisIndex: 1,
	        label: {
	            normal: {
	                show: true,
	                position: 'inside',
	                color:'#fff',
	            }
	        },
	        itemStyle: {
	            normal: {
	                color:'#ED7C30',
	            }
	        },
	        //data:[45, 94, 62, 89]
	        data:finishData
	    }]
	};
	equipLineChart.setOption(option);
}

// 质量指标
function uqcChartInit(data) {
	option2 = {
	    tooltip : {
	        trigger: 'axis',
	        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
	            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
	        }
	    },
	    grid:{	
	    	top:'0%',		
			left:'3%',	
			bottom: '5%',		
			containLabel: true,
		},
	    legend: {
	     	bottom: '5',
	        data: ['OK', 'NG'],
	          textStyle: {
	           color:'#fff',
	       },
	    },
	    xAxis:  {
	    	show: true,
		    type: 'value',
	        splitNumber:1,
	        max:'dataMax',
	        splitLine: {            
	            lineStyle: {
	                color: '#5A6275',
	            }
	        },
	        axisLine: {
	            show:'true',
	            lineStyle: {
	                color: '#fff',
	            }
	        },
	        axisLabel : {
				'interval' : 0,
				 show: false,
				 textStyle: {
                    color: '#fff',
                },
                 axisTick: {
		             show: false,
		        },
		        
			},    
	    },
	    yAxis: {
	        type: 'category',
	         axisTick: {
             	show: false,
        	 },
	         axisLabel: {
                textStyle: {
                    color: '#fff'
                }
	        },
	         axisLine: {
	            lineStyle: {
	                color: '#fff',
	            }
	        },
	         splitLine:{
	           show: false,
	        },
	         //data: ['w201701','w201702','w201703','w201704','w201705']
	         data:data.checkItemList,
	    },
	    series: [
	        {
	            name: 'OK',
	            type: 'bar',
	            stack: '总量',
	            barWidth: 15,
	        	color:'ED7C30',
	        	itemStyle: {
		            normal: {
		                color:'#599AD6',
		            }
		        },
	            label: {
	                normal: {
	                    show: true,
	                    position: 'inside'
	                }
	            },
	            //data: [320, 302, 301, 334, 390]
	            data:data.OKList
	        },
	        {
	            name: 'NG',
	            type: 'bar',
	            stack: '总量',
	            barWidth: 15,
	            label: {
	                normal: {
	                    show: true,
	                    position: 'inside'
	                }
	            },
	            itemStyle: {
		            normal: {
		                color:'#EC7C32',
		            }
		        },
	            //data: [120, 132, 101, 134, 90]
	            data:data.NGList
	        },	        
	    ]
	};
	
	// 为echarts对象加载数据
	qualityPerChart.setOption(option2);
}	
//设备状态
function setEquipmentState(data){
	var runQty=0;
	var alarmQty=0;
	var idleQty=0;
	var disconnectQty=0;	
	if(data.alarm == undefined){
		$("#alarm").html("报警："+0);
	}else{
		$("#alarm").html("报警："+data.alarm);
		alarmQty=data.alarm;		
	}
	
	if(data.run == undefined){
		$("#run").html("运行："+0);
	}else{
		$("#run").html("运行："+data.run);
		runQty =data.run;
	}
	
	if(data.idle == undefined){
		$("#idle").html("空闲："+0);
	}else{
		$("#idle").html("空闲："+data.idle);
		idleQty = data.idle;
	}
	
	if(data.disconnect == undefined){
		$("#disconnect").html("待机："+0);
	}else{
		$("#disconnect").html("待机："+data.disconnect);
		disconnectQty = data.disconnect;
	}
	var a = parseInt(runQty);
	var b = parseInt(runQty)+parseInt(alarmQty)+parseInt(disconnectQty)+parseInt(idleQty);
	var kdl =((a/b)*100).toFixed(2);
	
	$("#kdl").html("开动率:"+kdl+"%");
}

// 通知模板状态
function setTvStata(data) {
	var divHeight = $(".container").height() - $(".bottom").height()- $(".top").height();
	$(".lay").height(divHeight + "px");
	
	if (data == 0) {
		$("#lay").hide();
		$(".top").show();
		$(".body").show();
		$(".bottom").show();		
		$("#csd1").show();
		$("#csd2").show();
	} else {
		$(".lay").show();// 显示div
		$(".top").show();
		$(".bottom").show();
		
		$(".block").hide();
		$(".content-top").hide();
		$(".content-bottom").hide();
		
		$("#csd1").hide();
		$("#csd2").hide();
	}
}

//通知模板内容
function setTvContent(data){
	$("#equipmentModel2").children().remove();	
	$("#equipmentModel2").append(data);

}
// 机型
function setEquipType(data) {
	if (data == undefined) {
		$("#equipmentModel").html("机型：");
	} else {
		$("#equipmentModel").html("机型：" + data);
	}
}


//物料信息（表格）
function getMrlTableInfo(data,data2) {
	// 转换jason格式
	var dataObj = eval(data);
	var dataObj2 = eval(data2);
	$("#percent").html(dataObj2.percent);
	
	//$("#planQty-wipQty").html(dataObj2.planQty+"/"+dataObj2.wipQty);
	//$("#finishQty-transitQty").html(dataObj2.finishQty+"/"+dataObj2.transitQty);
	$("#planQty-wipQty").html(dataObj2.transitQty);
	$("#finishQty-transitQty").html(dataObj2.finishQty);
	// 遍历数组
	$.each(dataObj, function(j,item) {
		var opName_=$("#opName_"+j);
		//var opTransit_=$("#opTransit_"+j);
		var opFinish_=$("#opFinish_"+j);
		var opBar_=$("#opBar_"+j);
		$(opName_).text(item.opName);
		//$(opTransit_).text(item.opTransit);
		$(opFinish_).text(item.opFinish);
		
		if(dataObj2.planQty==0)
		{
		
		}else
		{
			var prgStandFloat = ((item.opFinish / dataObj2.transitQty)*100).toFixed(0);				
			prgStandFloat = prgStandFloat + "%" ;			
			$(opBar_).attr("style",'width: '+ prgStandFloat);//改变进度
			$(opBar_).html(prgStandFloat);
		}
		

	});
}

// 产线负责人
function setDutyMan(data) {
	
	var leader = data.leader;
	if (leader == undefined) {
		$("#director").text("-");
	} else {
		$("#director").text(data.leader);
	}
	var leaderTel = data.leaderTel;
	if (leaderTel == undefined) {
		$("#directorTel").text("-");
	} else {
		$("#directorTel").text(leaderTel);
	}

	$("#dutyImg").attr("src","http://" + serverIp + ":" + serverPort + "/" + serverProject + "/" + data.leaderImgContent);
}

//异常处理（获取最近一条异常信息）
function setLiValue(wrongs,wrongQty,dealingQty) {
	var worngArray = eval(wrongs);
	$("#eceptionQty").html(wrongQty);//异常数
	$("#dealingQty").html(dealingQty);//处理中
	$("#exception-content").children().remove();
	var content = "";
	if (wrongs == "" || wrongs == null) 
	{
	
	}else
	{
		var worngNums = worngArray.length;
		for (var i = 0; i < worngNums; i++) {			
			content = worngArray[i].workCellName + " " + worngArray[i].exceptionTypeName;
			$("#exception-content").append("<li id='exceptionContent'>"+content+"</li>");
			$('li :even').css('background','#64676E');
			$('li :odd').css('background','#81858E');			
		}
	}
	
}


//消息日志
var logTime = (new Date()).toLocaleString();
var errorTime = (new Date()).toLocaleString();
function getLog(){
    alert("接收:"+logTime+"\n错误:"+errorTime);
}

function changeLogo(){
	logTime= (new Date()).toLocaleString();
	var logoSrc = $("#epichus-logo")[0].src;
	var indexStr = logoSrc.indexOf("logo-epichhust1");
	var indexStr1 = logoSrc.indexOf("logo-epichhust2");
	if(indexStr != -1)
	{
		$("#epichus-logo").attr('src','../image/logo-epichhust2.png');
	}
	if(indexStr1 != -1)
	{
		$("#epichus-logo").attr('src','../image/logo-epichhust1.png');
	}
	
	var date = new Date();
	if (date.getHours() >= 8 && date.getHours() < 20) {//白班
		$("#shift").attr('src','../image/work.png');
	} else {//晚班
		$("#shift").attr('src','../image/night.png');
	}
}


var msgUrl = "ws://" + serverIp + ":" + serverPort + "/" + serverProject + "/webmsg/";
function onMessage(response){
	changeLogo();
	var message = response.responseBody;
	message = atmosphere.util.parseJSON(message);
	var msgData = $.parseJSON(message.text);
	//时间ajax获取一次即可 nowDate = msgData.nowDate;
	var equipStatusKanbanDataMap = msgData.equipStatusKanbanDataMap;
	var equipOEEDataMap = msgData.equipOEEDataMap;
	var equipFinishDataMap = msgData.equipFinishDataMap;
	
	var mrl = msgData.mrl;//机型
	var syncTime = msgData.syncTime;
	var syncEchart = msgData.syncEchart;
	var syncReload = msgData.syncReload;
	var syncTvContent = msgData.syncTvContent === 'true';
	var syncResourcePlan = msgData.syncResourcePlan === 'true';
	var syncLineDirector = msgData.syncLineDirector === 'true';
	var syncUnusualAlarm = msgData.syncUnusualAlarm === 'true';
	
	log( "syncTime:"+syncTime
		+"  syncTvContent:"+syncTvContent
		+"  syncResourcePlan:"+syncResourcePlan
		+"  syncLineDirector:"+syncLineDirector
		+"  syncUnusualAlarm:"+syncUnusualAlarm);
		
	if(syncTime){
		log("syncTime has done!");
		nowDate = msgData.nowDate;			
	}
	
	if(syncReload){
		log("reload page has done!");		
		window.location.reload();
	}
		
	//通知模板
	var tvState = msgData.tvState;
	setTvStata(tvState);
	if(tvState==1){//notice
		var tvContent = msgData.tvContent;	
		setTvContent(tvContent);		
		return;
	}
	
	if (mrl == undefined) {
		$("#equipmentModel").html("机型：");
	} else {
		$("#equipmentModel").html("机型："+mrl);
	}
	
	// 异常类容(获取最近一次的异常信息)
	var unusualAlarmMap = msgData.unusualAlarmMap;
	if (null != unusualAlarmMap){
		setLiValue(unusualAlarmMap.wrongs,unusualAlarmMap.wrongQty,unusualAlarmMap.dealingQty);
	}
		
	
	if (null != equipStatusKanbanDataMap) {
		changeEquipSvgData(equipStatusKanbanDataMap.dataList);
	}
	
	//设备稼动率
	if (null != equipOEEDataMap && null != equipLineChart) {
		equipLine(equipOEEDataMap,equipFinishDataMap);
	}
	
	var uqcDataMap = msgData.uqcDataMap;
	if (null != uqcDataMap){
		uqcChartInit(uqcDataMap);
	}
		
	// 工序名称
	var equipOp = msgData.equipOp;	
	if (syncResourcePlan) {
		log("syncResourcePlan equipOp has done!");
		$.each(equipOp, function(i) {
			var item = equipOp[i];
			$.each(svgDataArray, function(j) {
				if (item.equipName.indexOf(svgDataArray[j]) != -1) {
					var index = j < 9 ? "0" + (j + 1) : (j + 1);
					var op = document.getElementById("op" + index);
					op.textContent = item.opName;
				}
			});
		});
	}
	
	//物料管理	
	var mrlStateList = msgData.mrlStateList;
	var planCompMap = msgData.planCompMap;
	if(checkNull(mrlStateList) && checkNull(planCompMap))
		getMrlTableInfo(mrlStateList, planCompMap); 
	
	//加工信息
	var processMsgDataList = msgData.processMsgDataList;
	if(syncResourcePlan){
		log("syncResourcePlan processMsgDataList has done!");
		setProcessMsg(processMsgDataList);
	}
	
	//责任人
	var dutyManDataMap = msgData.dutyManDataMap;
	if(syncLineDirector && checkNull(dutyManDataMap)){
		log("syncLineDirector has done!");
		setDutyMan(dutyManDataMap);
	}
	
	//设备状态
	var stateMap = msgData.stateMap;
	setEquipmentState(stateMap);
	
 }
function onError(response){
	changeLogo();
	errorTime= (new Date()).toLocaleString()+"-E:"+JSON.stringify(response);
}

function checkNull(data) {
	if (data != null && data !== undefined) {
		return true;
	}

	return false;
}

function log(info){
	if(isDebug)
		console.log(new Date() + info);
}

function initWebSocketRequest() {
	initAtmosphere(msgUrl + showModul + "+" + type + "+" + workCenterGid + "+" + userName, onMessage, onError);
}

initWebSocketRequest(); 

